classdef ProcessSBE25xmlcon_dev < ProcessXMLCON_dev
  % Class for processing SBE 25 XMLCON files
  % Derived from ProcessXMLCON_dev
  % Constructor signature: xmlcon = ProcessSBE25xmlcon(wFO), arg optional,
  %                                 wFO = fileWrapper object
  %
  % Author: David Goldsmith, Wash. State Dept. of Ecology, dgol461@ecy.wa.gov
  % Release date: N/A
    
    properties
      % Inherited
%         wFO = {};
%         err = '';
%         data;
%         Sensors;
    end
    
    methods
        
        function xmlcon = ProcessSBE25xmlcon_dev(wFO) % Constructor
            if ~nargin
                wFO = '';
            end
            xmlcon = xmlcon@ProcessXMLCON_dev(wFO);
        end
        
        function unpackXMLCON(obj)
            if isfield(obj.data, 'Instrument_SensorArray_Sensor') && ...
               iscell(obj.data.Instrument_SensorArray_Sensor)
                obj.data.('Instrument_SensorArray') = struct;
                for c=obj.data.Instrument_SensorArray_Sensor(:)'
                    tmp = c{1};
                    if isstruct(tmp)
                        structs = {}; nstructs = {};
                        fields = fieldnames(tmp);
                        for field=fields(:)'
                            f = char(field);
                            if isstruct(tmp.(f))
                                structs{end+1} = tmp.(f);
                            else
                                nstructs{end+1} = f;
                            end
                        end
                        for n=nstructs(:)'
                            f = char(n);
                            for S=structs(:)'
                                s = S{1};
                                fields = fieldnames(s);
                                if ismember(f, fields) && ...
                                   isequal(tmp.(f), s.(f))
                                    tmp = rmfield(tmp, f);
                                else
                                    s.(f) = tmp.(f);
                                    tmp = rmfield(tmp, f);
                                end
                            end
                        end
                        fields = fieldnames(tmp);
                        f = char(fields{1});
                        obj.data.Instrument_SensorArray.(f) = s;
                    end
                end
                obj.data = rmfield(obj.data, 'Instrument_SensorArray_Sensor');
                fields = fieldnames(obj.data.Instrument_SensorArray);
                for field=fields(:)'
                    f = char(field);
                    [obj.data.Instrument_SensorArray.(f),~] = ...
                            flattenStruct({obj.data.Instrument_SensorArray.(f)});
                end
                obj.Sensors = obj.data.Instrument_SensorArray;
                obj.data = rmfield(obj.data, 'Instrument_SensorArray');
            end
        end
        
    end
    
end